<?
define(NORMAL_VERSION_DISPLAY,1);
define(COLOR_VERSION_DISPLAY,2);
define(TOUCH_VERSION_DISPLAY,3);
class Biz_Smarty extends Lib_SmartyTpl{
    /**
     * 设定输出的类型 touch:触屏版 color:彩色板
     * */
    public $version = TOUCH_VERSION_DISPLAY;
    /**
     * 设定cookie名称
     * */
    public $cookie_name = 'sohu_in_m_version';
    /**
     * 设定允许的版本号
     * */
    public $limit_version_array = array(NORMAL_VERSION_DISPLAY,COLOR_VERSION_DISPLAY,TOUCH_VERSION_DISPLAY);
    /**
     * 构造函数
     * */
    function __construct($version = null,$limit_version_array = array(1,2,3)){
        parent::__construct();
        $this->limit_version_array = $limit_version_array;
        $this->set_version($version,$limit_version_array);
    }
    /**
     * 设定版本
     * */
    public function set_version($version = null){
        if($version != null && in_array($version,$this->limit_version_array))
        $this->_set_version($version,true);
        else
        $this->_check_cookie($version);
    }
    /**
     * 路径指向
     * */
    public function get_file($resource_name,$version = null){
        $version == null && $version = $this->version;
        global $CFG;
        if(isset($CFG['version_location'][$version])){
            $this->assign('version_location',$CFG['version_location'][$version]);
            return $CFG['version_location'][$version]."/".$resource_name;
        }else{
            exit("未指定该版本对应的目录位置");
        }
    }
    /**
     * 输出
     * */
    function get($resource_name,$data = null,$cache_id = null,$compilie_id = null,$display = 'fetch'){
        parent::get($resource_name,$data,$cache_id,$compilie_id,$display);
    }
    /**
     * 输出
     * */
    function show($resource_name,$data = null,$cache_id = null,$compilie_id = null,$display = 'fetch'){
        $resource_name = $this->get_file($resource_name);
        parent::show($resource_name,$data,$cache_id,$compilie_id);
    }
    /**
     * ***********************
     * *******内部函数*********
     * ***********************
     * */
    /**
     * 检查COOKIE
     * */
    private function _check_cookie($version){
        if(!empty($_COOKIE[$this->cookie_name]) && in_array($_COOKIE[$this->cookie_name],$this->limit_version_array)){
            $this->_set_version($_COOKIE[$this->cookie_name],false);
        }else{
            $this->_check_WURFL();
        }
    }
    /**
     * 检查wful
     * */
    private function _check_WURFL($detail = false){
        $wurfl = new Lib_MobileVersion();
        $wurfl->set_default_version(max($this->limit_version_array));
        $version = $wurfl->check_version();
        if($detail) $wurfl->wurfl_info();
        if(in_array($version,$this->limit_version_array)){
            exit();
            return $version;
        }else{
            return max($this->limit_version_array);
        }
    }
    /**
     * 设置版本
     * */
    private function _set_version($version,$set_cookie = false){
        $set_cookie === true && setcookie($this->cookie_name,$version,time()+365*24*3600);
        $this->version = $version;
    }
}
?>